package com.hangwei.paperless.zxing;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.qrlibrary.qrcode.activity.DefaultQRScanActivity;
import com.hangwei.paperless.R;


/**
 * Created by 张龙臣 on 2020/11/21.
 */

public class QRCodeScanActivity extends DefaultQRScanActivity {
//    @Override
//    public void onCreate(Bundle icicle) {
//        super.onCreate(icicle);
//        ViewGroup.LayoutParams layoutParams = scanCropView.getLayoutParams();
//        layoutParams.height = Tools.dp2px(340);
//        layoutParams.width = Tools.dp2px(340);
//        scanCropView.setLayoutParams(layoutParams);
//        View childAt =  scanCropView.getChildAt(0);
//        ViewGroup.LayoutParams layoutParams1 = childAt.getLayoutParams();
//        layoutParams1.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        layoutParams1.height = ViewGroup.LayoutParams.MATCH_PARENT;
//        childAt.setLayoutParams(layoutParams1);
//    }

    @Override
    protected void initCustomViewAndEvents() {

    }
    @Override
    protected void initTitleViewEvents() {
        capture_scan_back_iv = (ImageView) findViewById(R.id.ivBack);
        capture_scan_back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        capture_flashlight = (ImageView) findViewById(R.id.ivLamp);
        capture_flashlight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isFlashlightOpen) {
                    cameraManager.setTorch(false); // 关闭闪光灯
                    isFlashlightOpen = false;
                    capture_flashlight.setImageResource(R.drawable.lamp_close);
                } else {
                    cameraManager.setTorch(true); // 打开闪光灯
                    isFlashlightOpen = true;
                    capture_flashlight.setImageResource(R.drawable.lamp_open);
                }
            }
        });
    }
    @Override
    protected void onAlbumResult(int requestCode, int resultCode, String recode) {
        Bundle bundle = new Bundle();
        bundle.putString("result", recode);
        startActivity(new Intent(QRCodeScanActivity.this, ResultActivity.class).putExtras(bundle));
        finish();
    }

    @Override
    protected void handleDecodeResult(String rawResult, Bundle bundle) {

        int type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            bundle.putString("result", rawResult);
            Intent intent = new Intent(QRCodeScanActivity.this, ResultActivity.class);
            intent.putExtra("type", getIntent().getIntExtra("type", 0));
            startActivity(intent.putExtras(bundle));
        } else if (type == 1) {
//            bundle.putString("result", rawResult);
//            Intent intent = new Intent(QRCodeScanActivity.this, ResultActivity1.class);
//            intent.putExtra("type", getIntent().getIntExtra("type", 0));
//            startActivity(intent.putExtras(bundle));
        } else if (type == 2) {
            Intent intent = getIntent();
            intent.putExtra("result", rawResult);
            setResult(101, intent);
        }
        finish();
    }

    @Override
    protected int getTitleCustomLayout() {
        return R.layout.base_qrscan_title;
    }
}
